import { Component,ViewChild,  Inject, ChangeDetectorRef } from '@angular/core';
import { IonicPage, NavController, Content,Refresher } from 'ionic-angular';

import * as PageConstant from '../../constant/pageConstant';
import { Subscription } from "rxjs/Subscription";
import { AlertService } from '../../providers/utils/alert.service';

/**
 * 我的
 */
@IonicPage()
@Component({
  selector: 'page-mine',
  templateUrl: 'mine.html'
})
export class MinePage {

  // 用户信息
  private idInfo: any;
  // chart 相关配置
  // private charts;

  // 若下来未完成不能点击下一步
  private isCanNextNumber: number= 0;
  @ViewChild(Content) content: Content;
  @ViewChild(Refresher) refresher: Refresher;

  // 监听 用户信息
  private userSubscription: Subscription;
  constructor(
    @Inject('user') private userService,
    public navCtrl: NavController,
    private ref: ChangeDetectorRef,
    private alertCtrl: AlertService,
  )
  {
  }
  /*** 初始化Chart.js
  * */
  /*initChart()
  {
    this.charts= {
      type: 'doughnut',
      data: {
        datasets: [{
          data: [100,0],
          backgroundColor: [
            '#2DA0B5',
            '#efeff4'
          ],
          hoverBackgroundColor:[
            '#2DA0B5',
            '#efeff4'
          ],
          hoverBorderColor: '#efeff4',
          borderColor: '#efeff4'
        }],
        /!*labels:[
          '剩余',
          '已借'
        ]*!/
      },
      options:  {
        cutoutPercentage: 80,
      },
    }
  }*/
  ionViewDidLoad()
  {
    /*this.refresher._top = this.content.contentTop+ 'px';
    this.refresher.state = 'ready';
    this.refresher._onEnd();*/
    this.userService.getUserInfoFromBack(null);
  }

  /*** 初始化数据
   * */
  initData()
  {
    this.userSubscription= this.userService.getUser()
      .filter(user=> user.userId)
      .subscribe(user => {
        if(!!user.idState && !!user.bankState){
          this.isCanNextNumber= 2; // 2代表两个认证都通过
          this.idInfo= {...user.idInfo}
        }else{
          this.isCanNextNumber= 1; //1代表未通过
        }
        if(!!user.idState && !!user.contactState && !!user.bankState
        && !!user.mobileState){
        }
        this.ref.detectChanges();
      })
  }
  /*** 页面进入
   * */
  ionViewWillEnter()
  {
    this.initData();
  }

  /*** 页面即将离开
   * */
  ionViewWillLeave()
  {
    if(typeof this.userSubscription !== 'undefined')
      this.userSubscription.unsubscribe();
  }

  /*** 打开借款记录
   * */
  public doBorrowOrder(): void
  {
    this.navCtrl.push(PageConstant.BorrowOrderPage);
  }

  /*** 打开绑定银行卡
   * */
  public doBindBankCard(): void
  {
    if(!this.isCanNextNumber) return;

    if(this.isCanNextNumber===1){
      this.alertCtrl.createMessageAlert('提示','尚未认证,是否立即去?').then(status=>{
        if(!status) return;
        this.navCtrl.push(PageConstant.AuthenticationCenterPage);
      });
      return;
    }
    if(this.isCanNextNumber===2){
      this.navCtrl.push(PageConstant.BindBankCardPage);
      return;
    }
  }

  /*** 打开认证中心
   * */
  public doAuthenticationCenter(): void
  {
    this.navCtrl.push(PageConstant.AuthenticationCenterPage);
  }

  /*** 打开帮助中心
   * */
  public doHelpCenter(): void
  {
    this.navCtrl.push(PageConstant.HelpCenterPage);
  }

  /*** 打开设置
   * */
  public doSettings(): void
  {
    this.navCtrl.push(PageConstant.SettingsPage);
  }

  /*** 上拉刷新
   * */
  executeRefresh(refresher: Refresher) {
    this.isCanNextNumber= 0;
    this.userService.getUserInfoFromBack(refresher);
  }

}





